﻿' LibWowAPI
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Drawing
Imports System.Linq
Imports System.Runtime.CompilerServices
Imports roncliProductions.LibWowAPI.Data
Imports roncliProductions.LibWowAPI.Enums

Namespace roncliProductions.LibWowAPI.Extensions

    Public Module DateExtensions

        ''' <summary>
        ''' Returns a collection from an IEnumerable object.
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="enumerable"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> Public Function ToCollection(Of T)(enumerable As IEnumerable(Of T)) As Collection(Of T)
            If enumerable Is Nothing Then
                Throw New ArgumentException("The enumeration is null.", "enumerable")
            End If

            Dim collection As New Collection(Of T)
            For Each obj As T In enumerable
                collection.Add(obj)
            Next
            Return collection
        End Function

        ''' <summary>
        ''' Adds a range of items to a collection.
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="collection"></param>
        ''' <param name="enumerable"></param>
        ''' <remarks></remarks>
        <Extension()> Public Sub AddRange(Of T)(collection As Collection(Of T), enumerable As IEnumerable(Of T))
            If collection Is Nothing Then
                Throw New ArgumentException("The collection is null.", "collection")
            End If

            If enumerable Is Nothing Then
                Throw New ArgumentException("The enumeration is null.", "enumerable")
            End If

            For Each obj In enumerable
                collection.Add(obj)
            Next
        End Sub

        ''' <summary>
        ''' Returns the UTC date of a Blizzard timestamp.
        ''' </summary>
        ''' <param name="lngTimestamp"></param>
        ''' <returns></returns>
        ''' <remarks>Blizzard timestamps are represented as the number of milliseconds since the Unix epoch of 1/1/1970.</remarks>
        <Extension()> Public Function BlizzardTimestampToUTC(lngTimestamp As Long) As Date
            Return New Date(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(lngTimestamp)
        End Function

        ''' <summary>
        ''' Returns a Color object from a hexidecimal ARGB color string.
        ''' </summary>
        ''' <param name="strColor"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> Public Function ArgbHexToColor(strColor As String) As Color
            Return Color.FromArgb(Convert.ToInt32(strColor, 16))
        End Function

        ''' <summary>
        ''' Retrieves the character class for the specific region and language from the specified Class ID.
        ''' </summary>
        ''' <param name="intClassID"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> Public Function GetClass(intClassID As Integer) As CharacterClasses.Class
            Dim ccClasses = New CharacterClasses.CharacterClasses()
            Dim varClass = ccClasses.Classes.Where(Function(c) c.ID = intClassID)
            If varClass.Count = 0 Then
                Return Nothing
            Else
                Return varClass.First()
            End If
        End Function

        ''' <summary>
        ''' Retrieves the character race for the specific region and language from the specified Race ID.
        ''' </summary>
        ''' <param name="intRaceID"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> Public Function GetRace(intRaceID As Integer) As CharacterRaces.Race
            Dim crRaces = New CharacterRaces.CharacterRaces()
            Dim varRace = crRaces.Races.Where(Function(r) r.ID = intRaceID)
            If varRace.Count = 0 Then
                Return Nothing
            Else
                Return varRace.First()
            End If
        End Function

        ''' <summary>
        ''' Returns the RealmType from Blizzard's string representation.
        ''' </summary>
        ''' <param name="strType"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> Public Function GetRealmType(strType As String) As RealmType
            Select Case strType
                Case "pve"
                    Return RealmType.PvE
                Case "pvp"
                    Return RealmType.PvP
                Case "rp"
                    Return RealmType.RP
                Case "rppvp"
                    Return RealmType.RPPvP
                Case Else
                    Return RealmType.Unknown
            End Select
        End Function

        ''' <summary>
        ''' Returns the PowerType from Blizzard's string representation.
        ''' </summary>
        ''' <param name="strPowerType"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> Public Function GetPowerType(strPowerType As String) As PowerType
            Select Case strPowerType
                Case "energy"
                    Return PowerType.Energy
                Case "focus"
                    Return PowerType.Focus
                Case "mana"
                    Return PowerType.Mana
                Case "rage"
                    Return PowerType.Rage
                Case "runic-power"
                    Return PowerType.RunicPower
                Case Else
                    Return PowerType.Unknown
            End Select
        End Function

        ''' <summary>
        ''' Returns the Side from Blizzard's string representation.
        ''' </summary>
        ''' <param name="strSide"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> Public Function GetSide(strSide As String) As Side
            Select Case strSide
                Case "alliance"
                    Return Side.Alliance
                Case "horde"
                    Return Side.Horde
                Case Else
                    Return Side.Unknown
            End Select
        End Function

        ''' <summary>
        ''' Retrieves the item class for the specific region and language from the specified Item Class ID.
        ''' </summary>
        ''' <param name="intItemClass"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> Public Function GetItemClass(intItemClass As Integer) As ItemClasses.Class
            Dim icItemClasses = New ItemClasses.ItemClasses()
            Dim varItemClass = icItemClasses.Classes.Where(Function(c) c.Class = intItemClass)
            If varItemClass.Count = 0 Then
                Return Nothing
            Else
                Return varItemClass.First()
            End If
        End Function

    End Module

End Namespace
